import { BrowserWindow, app, ipcMain } from "electron";
import appMenu from "./menus/app";
import path from "node:path";
import { IpcEvents } from "../common/ipcEvents";
import { ThemeColor } from "../common/theme";

let win: BrowserWindow | null = null;
const preloadPath = path.join(__dirname, "preload.cjs");

app.whenReady().then(() => {
  app.applicationMenu = appMenu;

  ipcMain.on(IpcEvents.SET_THEME_COLOR, (event, color: ThemeColor) => {
    win?.setTitleBarOverlay({
      color: color.titleBar.background,
      symbolColor: color.titleBar.foreground,
    });
  });

  createWindow();
});

function createWindow() {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    show: false,
    frame: false,
    titleBarStyle: "hidden",
    titleBarOverlay: {
      color: "green",
    },
    trafficLightPosition: { x: 8, y: 8 },
    webPreferences: {
      preload: preloadPath,
      nodeIntegration: false,
      contextIsolation: true,
      enableWebSQL: true,
    },
  });
  win.loadURL("http://localhost:5173").then(() => {
    win?.show();
    win?.webContents?.openDevTools({ mode: "undocked" });
  });
}
